package com.jsnkj.basic.fragment

import androidx.lifecycle.MutableLiveData
import com.czhj.sdk.common.utils.Md5Util
import com.jsnkj.basic.bean.AdrewardsEntity
import com.jsnkj.basic.bean.ErrorEntity
import com.jsnkj.basic.bean.UserLoginEntity
import com.jsnkj.basic.config.RewardsConfig
import com.jsnkj.basic.http.HttpApiService
import com.jsnkj.basic.mvvm.HttpViewModel
import com.jsnkj.basic.util.SharePreferencesUtil
import com.jsnkj.basic.util.jsonToString
import com.yang.http.HttpRequest
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import java.util.TreeMap

class RedPacketViewModel : HttpViewModel() {


    val messages = arrayOf(
        "抢到大红包，好运连连一整年！",
        "红包来袭，速度要快，手慢无！",
        "今晚红包雨，大家准备好了吗？",
        "抢红包，看谁眼疾手快运气好！",
        "分享红包，传递快乐和好运！",
        "抢到红包，感觉就像中了大奖！",
        "红包大战开始，大家加油哦！",
        "抢红包，比比谁的手气最佳！",
        "抢红包，让节日气氛更加热烈！",
        "红包抢得欢，快乐你我他！",
        "大红包来袭，快来抢，好运随你行！",
        "手快有，手慢无，红包等你来拿！",
        "红包雨下不停，你准备好迎接好运了吗？",
        "眼明手快抢红包，快乐好运一起到！",
        "分享喜悦，传递祝福，红包让你我更亲近",
        "抢到红包，幸福满满，好运连连！",
        "红包大战一触即发，你准备好加入战斗了吗",
        "比手速，拼运气，看谁抢红包最厉害！",
        "节日红包大放送，让快乐加倍，好运不断！",
        "抢红包，乐翻天，幸福感觉无极限！",
        "红包如雨，好运随行，快来一起抢！",
        "速度与激情，红包抢夺战，你不可错过！",
        "红包大派送，快乐不停歇，好运自然来！",
        "抢红包，传递欢笑，让好运与你同行！",
        "红包大战，谁与争锋，快来一试身手！",
        "好运红包，快来试试你的手气！",
        "红包雨降临，快乐和好运，一个都不能少！",
        "抢红包，比比谁的手更快，运气更好！",
        "红包大放送，让节日的欢乐更加浓厚！",
        "快乐抢红包，好运自然来，你还在等什么？",
        "红包大爆炸，好运连连，快乐不停！",
        "抢红包，速度决定一切，快来挑战！",
        "红包来袭，快乐和好运，一个都不落下！",
        "分享红包，让好运传递给每一个人！",
        "抢红包，好运随你行，快乐无处不在！",
        "红包大战，快来加入抢夺的行列！",
        "比手速，赛运气，抢红包，快乐无限！",
        "红包雨，快乐洒满地，好运随你行！",
        "抢红包，让节日的气氛更加热烈！",
        "红包抢不停，快乐传千里，好运伴你行！"
    )


    val usernames = arrayOf(
        "Linda",
        "Alex",
        "Jordan",
        "Michael",
        "Sarah",
        "Daniel",
        "Jessica",
        "Ryan",
        "Emma",
        "Olivia",
        "Noah",
        "Sophia",
        "Ethan",
        "吴小莉",
        "郑大勇",
        "徐小飞",
        "郭小美",
        "林小刚",
        "陈小霞",
        "午夜幽蓝",
        "篮球小子",
        "摇滚少女",
        "编程师",
        "旅行一号",
        "书海航",
        "乐精灵",
        "马拉跑者",
        "围棋高手",
        "爱好者",
        "探险家",
        "小周",
        "小吴",
        "小郑",
        "小徐",
        "小高",
        "小林",
        "阿敏",
        "阿涛",
        "阿芳",
        "阿华",
        "阿静",
        "阿丽",
        "阿杰",
    )

    val avatars = arrayOf(
        "https://pic1.zhimg.com/80/v2-055990bb7ff9073a61ea666320b9ff88_720w.webp",
        "https://pic3.zhimg.com/80/v2-b956e3aafe589237f2ccc195f572a5e2_720w.webp",
        "https://pic2.zhimg.com/80/v2-cdc555b4fe86f2d9323639d579a726bd_720w.webp",
        "https://pic1.zhimg.com/80/v2-a001f0786047612be7511c3f5d30c8c4_720w.webp",
        "https://pic4.zhimg.com/80/v2-398b2c260e092bcc45b19bd46a3a620f_720w.webp",
        "https://pic2.zhimg.com/80/v2-58c51ca7a92453d69da266b922bf2f91_720w.webp",
        "https://pic3.zhimg.com/80/v2-bd8b99e57c391b9342bd3c84f5eae97a_720w.webp",
        "https://pic4.zhimg.com/80/v2-0eb8160ad7c6c3615a11762a86e41393_720w.webp",
        "https://pic4.zhimg.com/80/v2-3b7406b6abd67cd1404a314e11c362ff_720w.webp",
        "https://pic1.zhimg.com/80/v2-0b7fc8388ebe3bd5f6f5fbb49e646258_720w.webp",
        "https://pic3.zhimg.com/80/v2-aec3b6bd9deb45d6a6c823ef38761a9a_720w.webp",
        "https://pic1.zhimg.com/80/v2-e9b9413fed02106cce1b592485adc4e8_720w.webp",
        "https://pic4.zhimg.com/80/v2-09d815d7895731dc094d50b3576ffd0b_720w.webp",
        "https://pic2.zhimg.com/80/v2-b4f54ce416c52e35f8c139b89926d4b9_720w.webp",
        "https://pic1.zhimg.com/80/v2-fefbed5b8a1d5d47695c6d333f62c9f8_720w.webp",
        "https://pic2.zhimg.com/80/v2-af48dd37ec652858d4b62135e30c8bf9_720w.webp",
        "https://pic4.zhimg.com/80/v2-a0ec3877653b4a88ea4c16fbc4666263_720w.webp",
        "https://pic4.zhimg.com/80/v2-3d81f3f2f632f03d44950201d2a6f72f_720w.webp",
        "https://pic1.zhimg.com/80/v2-58e8745691264ce49dd4f60f9ced76c4_720w.webp",
        "https://pic2.zhimg.com/80/v2-0c43c2e13d8b62ac3a7144c2b3faae91_720w.webp",
        "https://pic2.zhimg.com/80/v2-1f5365acfd4b7952d08a08bb1944ee51_720w.webp",
        "https://pic3.zhimg.com/80/v2-37e6abe31819f6f8e1897cc90170f40a_720w.webp",
        "https://pic4.zhimg.com/80/v2-efd058716583af6ed8b48076987815a3_720w.webp",
        "https://pic4.zhimg.com/80/v2-0f85ca88d2e9d2975d529eba356c87c3_720w.webp",
        "https://pic3.zhimg.com/80/v2-59800d05cb1e3eacb82a9aca588d3d96_720w.webp",
        "https://pic3.zhimg.com/80/v2-9ba2ab6b17101aa1473bad0ebe98e22e_720w.webp",
        "https://pic3.zhimg.com/80/v2-d4095a58279e575f4b41756cd4631ce6_720w.webp",
    )


    var postAdreWardsResult: MutableLiveData<AdrewardsEntity> = MutableLiveData()

    var errorResult: MutableLiveData<ErrorEntity> = MutableLiveData()

    /**
     * 上传明细
     */
    fun postAdreWards() {

        launchHttp({
            val postMap = TreeMap<String, Any> { p0, p1 -> p0.compareTo(p1) }

            RewardsConfig.beanList = ArrayList()

            for ((_, bean) in SharePreferencesUtil.getInstance()
                .getAdPostTypeBean().list.withIndex()) {
                RewardsConfig.beanList.add(bean)
            }
            postMap["details"] = RewardsConfig.beanList.jsonToString()

            postMap["token"] = com.jsnkj.basic.util.AESUtils.getUUID(32)
            postMap["timestamp"] = System.currentTimeMillis() / 1000

            val md =
                Md5Util.md5(postMap["token"].toString() + postMap["details"] + postMap["timestamp"])
            postMap["signature"] = md
            HttpRequest.getService(HttpApiService::class.java).postAdreWards(
                com.jsnkj.basic.util.AESUtils.encrypt(postMap.jsonToString()).jsonToString()
                    .toRequestBody("application/json;charset=UTF-8".toMediaTypeOrNull())
            )
        }, onResult = { it ->
            com.jsnkj.basic.util.LogUtils.e("上传明细 请求成功：$it")
            postAdreWardsResult.value = it
        }, onFailed = { code, msg ->
            errorResult.value = ErrorEntity.getInstance(code, msg)
        })
    }


    var postLoginResult: MutableLiveData<UserLoginEntity> = MutableLiveData()


    /**
     * 获取用户信息
     */
    fun postAuthUserInfo() {
        launchHttp({
            HttpRequest.getService(HttpApiService::class.java).postAuthUserInfo()
        }, onResult = { it ->
            com.jsnkj.basic.util.LogUtils.e("请求成功：$it")
            postLoginResult.value = it
        }, onFailed = { code, msg ->
            com.jsnkj.basic.util.LogUtils.e(" 获取用户信息 - onFailed: $code, $msg")
        }) {
            //LogUtils.e("请求成功", "postAppInit onComplete")
        }
    }


    var postAdWardsReceiveResult: MutableLiveData<String> = MutableLiveData()

    /**
     * 用户领取红包奖励
     */
    fun postAdreWardsReceive() {
        launchHttp({
            HttpRequest.getService(HttpApiService::class.java).postAdreWardsReceive()
        }, onResult = { it ->
            com.jsnkj.basic.util.LogUtils.e("用户领取红包奖励 请求成功：$it")
            postAdWardsReceiveResult.value = "红包已成功领取"
        }, onFailed = { code, msg ->
            com.jsnkj.basic.util.LogUtils.e(" 用户领取红包奖励失败 - onFailed: $code, $msg")
            errorResult.value = ErrorEntity.getInstance(code, msg)
        }) {
            //LogUtils.e("请求成功", "postAppInit onComplete")
        }
    }
}